<html>
<head><meta charset="utf-8"><title>Elided lifetime in return type · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Elided.20lifetime.20in.20return.20type.html">Elided lifetime in return type</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218619238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Elided%20lifetime%20in%20return%20type/near/218619238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dmitry Murygin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Elided.20lifetime.20in.20return.20type.html#218619238">(Dec 02 2020 at 21:02)</a>:</h4>
<p>Hello to all!<br>
I'm new in Rust community and I'm trying to solve one issue, but there is a situation I'm not sure about:<br>
What the expected result of compiling this function:</p>
<div class="codehilite"><pre><span></span><code>fn f&lt;&#39;a, I&gt;(mut i: I) -&gt; &amp;i32
where
    I: Iterator&lt;Item = &amp;&#39;a i32&gt;
{
    loop {
        match i.next() {
            Some(x) =&gt; return x,
            None =&gt; continue
        }
    }
}
</code></pre></div>
<p>The current compiler says that it is necessary to put explicit lifetime for return type, but maybe here should be used rules of eliding lifetimes?</p>



<a name="218647022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Elided%20lifetime%20in%20return%20type/near/218647022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Elided.20lifetime.20in.20return.20type.html#218647022">(Dec 03 2020 at 02:24)</a>:</h4>
<p>I don't think the lifetime elision rules apply because there is no self type</p>



<a name="218647132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Elided%20lifetime%20in%20return%20type/near/218647132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Elided.20lifetime.20in.20return.20type.html#218647132">(Dec 03 2020 at 02:26)</a>:</h4>
<p>Given the text exactly as written, I think <code>&amp;i32</code> should be an error, because the elision rules don't apply and hence the reference doesn't refer to anything. It should be written <code>&amp;'a i32</code></p>



<a name="218647168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Elided%20lifetime%20in%20return%20type/near/218647168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Elided.20lifetime.20in.20return.20type.html#218647168">(Dec 03 2020 at 02:27)</a>:</h4>
<p>(compiling it now, it looks like the rust compiler agrees with me)</p>



<a name="218647259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Elided%20lifetime%20in%20return%20type/near/218647259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Elided.20lifetime.20in.20return.20type.html#218647259">(Dec 03 2020 at 02:29)</a>:</h4>
<p>If this were to be written with elided lifetime syntax, it should be</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">f</span><span class="o">&lt;</span><span class="n">I</span><span class="o">&gt;</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">i</span>: <span class="nc">I</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="kt">i32</span>
<span class="nc">where</span><span class="w"></span>
<span class="w">    </span><span class="n">I</span>: <span class="nb">Iterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="kt">i32</span><span class="o">&gt;</span><span class="w"></span>
</code></pre></div>
<p>but apparently lifetimes can never be elided in trait bounds, which I guess is reasonable</p>



<a name="218647447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Elided%20lifetime%20in%20return%20type/near/218647447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Elided.20lifetime.20in.20return.20type.html#218647447">(Dec 03 2020 at 02:33)</a>:</h4>
<p>I haven't tried this before, but apparently it is valid to use elided lifetimes only partially, as in</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">f</span><span class="o">&lt;'</span><span class="na">a</span><span class="p">,</span><span class="w"> </span><span class="o">'</span><span class="na">b</span><span class="p">,</span><span class="w"> </span><span class="n">I</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">b</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">i</span>: <span class="nc">I</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="kt">i32</span>
    <span class="nc">where</span><span class="w"> </span><span class="n">I</span>: <span class="nb">Iterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="kt">i32</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">panic</span><span class="o">!</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>where the <code>&amp;i32</code> gets lifetime <code>'b</code>. Personally, I would try to avoid this; if you give a lifetime a name you should use it everywhere it applies</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>